namespace Mole
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using WeifenLuo.WinFormsUI.Docking;

    public class FrmMessage : DockContent
    {
        public bool _errors;
        public bool _info;
        public bool _warnings;
        private BindingSource bindingSource1;
        private IContainer components;
        private int count;
        private DataGridView dataGridView1;
        private DataSet dataset = new DataSet();
        private Panel panel1;
        private DataTable table = new DataTable("Alerts");

        public FrmMessage()
        {
            this.InitializeComponent();
            DataColumn column = new DataColumn("Type", typeof(string));
            DataColumn column2 = new DataColumn("Code", typeof(string));
            DataColumn column3 = new DataColumn("Description", typeof(string));
            this.table.Columns.Add(column);
            this.table.Columns.Add(column2);
            this.table.Columns.Add(column3);
            this.dataset.Tables.Add(this.table);
            this.dataGridView1.DataSource = this.dataset.Tables["Alerts"].DefaultView;
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            AlertOnEvent.ErrorAlertOnEventHandler += new EventHandler<AlertOnEventArgs>(this.ErrorAlertOnEvent_ErrorAlertOnEventHandler);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void ErrorAlertOnEvent_ErrorAlertOnEventHandler(object sender, AlertOnEventArgs e)
        {
            if (this.InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(delegate() { ShowMessage(e.Message, e.Type, e.Host, e.Description); }));
            }
            else
            {
                this.ShowMessage(e.Message, e.Type, e.Host, e.Description);
            }

            //if (this.InvokeRequired)
            //{
            //    if (this._errors && (e.Message == AlertOnEventArgs.MsgType.Error))
            //    {
            //        this.ShowMessage(e.Type, e.Host, e.Description);
            //    }
            //    else if (this._info && (e.Message == AlertOnEventArgs.MsgType.Info))
            //    {
            //        this.ShowMessage(e.Type, e.Host, e.Description);
            //    }
            //    else if (this._warnings && (e.Message == AlertOnEventArgs.MsgType.Warning))
            //    {
            //        this.ShowMessage(e.Type, e.Host, e.Description);
            //    }
            //}
            //else
            //{
            //    if (this._errors && (e.Message == AlertOnEventArgs.MsgType.Error))
            //    {
            //        this.ShowMessage(e.Type, e.Host, e.Description);
            //    }
            //    else if (this._info && (e.Message == AlertOnEventArgs.MsgType.Info))
            //    {
            //        this.ShowMessage(e.Type, e.Host, e.Description);
            //    }
            //    else if (this._warnings && (e.Message == AlertOnEventArgs.MsgType.Warning))
            //    {
            //        this.ShowMessage(e.Type, e.Host, e.Description);
            //    }
            //}
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            this.panel1 = new Panel();
            this.dataGridView1 = new DataGridView();
            this.bindingSource1 = new BindingSource(this.components);
            this.panel1.SuspendLayout();
            ((ISupportInitialize)this.dataGridView1).BeginInit();
            ((ISupportInitialize)this.bindingSource1).BeginInit();
            base.SuspendLayout();
            this.panel1.BorderStyle = BorderStyle.Fixed3D;
            this.panel1.Controls.Add(this.dataGridView1);
            this.panel1.Dock = DockStyle.Fill;
            this.panel1.Location = new Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new Size(0x3f8, 0x68);
            this.panel1.TabIndex = 0;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AllowUserToOrderColumns = true;
            this.dataGridView1.BackgroundColor = SystemColors.Window;
            this.dataGridView1.BorderStyle = BorderStyle.None;
            this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Dock = DockStyle.Fill;
            this.dataGridView1.Location = new Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.RowTemplate.Height = 0x18;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new Size(0x3f4, 100);
            this.dataGridView1.TabIndex = 0;
            base.AutoHidePortion = 0.15;
            base.AutoScaleDimensions = new SizeF(8f, 16f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x3f8, 0x68);
            base.Controls.Add(this.panel1);
            this.Font = new Font("Microsoft Sans Serif", 7.8f, FontStyle.Regular, GraphicsUnit.Point, 0);
            base.HideOnClose = true;
            base.Name = "FrmMessage";
            base.ShowHint = DockState.DockBottomAutoHide;
            this.Text = "Messages";
            this.panel1.ResumeLayout(false);
            ((ISupportInitialize)this.dataGridView1).EndInit();
            ((ISupportInitialize)this.bindingSource1).EndInit();
            base.ResumeLayout(false);
        }


        private void ShowMessage(AlertOnEventArgs.MsgType message, string type, string host, string desc)
        {

            DataRow row = this.table.NewRow();
            row["Type"] = type;
            row["Code"] = host;
            row["Description"] = desc;
            this.table.Rows.Add(row);
            this.count++;
            if (message == AlertOnEventArgs.MsgType.Error && this._errors)
            {
                this.Text = string.Format("{0} Errors", this.count);
                base.Show(); //popup form
            }
            else if (message == AlertOnEventArgs.MsgType.Info && this._info)
            {
                this.Text = string.Format("{0} Info", this.count);
            }
            else if (message == AlertOnEventArgs.MsgType.Warning && this._warnings)
            {
                this.Text = string.Format("{0} Warnings", this.count);
            }
        }
    }
}

